package com.example.xx.controller;


import com.example.xx.pojo.Article;
import com.example.xx.service.ArticleService;

import com.example.xx.util.Code;
import com.example.xx.util.PageUtil;
import com.example.xx.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.QueryAnnotation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping
public class ArticleController {
    @Autowired
    private ArticleService articleDao;
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @PostMapping("/guanli")
    public Result cha(@RequestBody PageUtil pageUtil){

        PageInfo<Article> cha = articleDao.cha(pageUtil);

        if (cha != null){
            System.out.println(cha);
            return new Result(Code.GET_OK,cha,"查询成功");
        }else {
            return new Result(Code.GET_ERR,"查询失败");
        }
    }
    //假删除
    @RequestMapping("/guanlishan/{id}")
    public Result shanchu(@PathVariable("id")Integer id){

        int shanchu = articleDao.shanchu(id);
        if (shanchu > 0){
            return new Result(Code.DELETE_OK,"删除成功");
        }else {
            return new Result(Code.DELETE_ERR,"删除失败");
        }
    }
    @PostMapping("/xinzeng")
    public Result tianjia(@RequestBody Article article){
        int tianjia = articleDao.tianjia(article);
        if (tianjia > 0){
            return new Result(Code.SAVE_OK,"添加成功");
        }else {
            return new Result(Code.SAVE_ERR,"添加失败");
        }
    }
    @PostMapping("/xiugai")
    public Result update(@RequestBody Article article){
        int update = articleDao.update(article);
        if (update > 0){
            return new Result(Code.UPDATE_OK,"修改成功");
        }else {
            return new Result(Code.UPDATE_ERR,"修改失败");
        }
    }

}
